﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Common;
using WarehouseManagement;
using WarehouseManagement.Header;

namespace WarehouseManagement.BO
{
class DeXuatVatTuBO
{
	private const string	TB_DXVT = "DeXuatVatTu",
							DXVTID	= "DXVTID";
	CommonDO cmDO = new CommonDO();

	public string[] colNames = new string[]
						{ "TenVatTu"					
						  ,"NKID"
						  ,"ShortID"
						  ,"GocID"
						  ,"Ngang"
						  ,"Cao"
						  ,"Sau"
						  ,"SoLuong"
						  ,"NVDeXuat"
						  ,"NgayDeXuat"
						  ,"GhiChu"
						  ,"Status"
						  ,"BoPhanNhan"
						  ,"Version"
						  ,"DXVTID"
						  ,"HMSPID"
						  ,"DHID"
						  ,"VTID"						
						};	
	
	public string getPhongBanInfo( SqlConnection conn, ref DataTable tbPhongBan )
	{
		string cmd = "Select * from PhongBan";
		return cmDO.getTable( conn, ref tbPhongBan, cmd );
	}
	
	public string getVatTu( SqlConnection conn, ref DataTable tbVatTu )
	{
		string cmd = "select TenVatTu, NhomVT + NKID as NKID, ShortID, GocID, Ngang, Cao, Sau, DonVi, VTID  from VATTU";
		return cmDO.getTable(conn, ref tbVatTu, cmd);
	}
	
	public string getDonHang(SqlConnection conn, ref DataTable tbDonHang,
								string sDateFrom, string sDateTo)
	{
		string cmd = "Select TenKhachHang+ ' ( MS: ' + MaDonHang + ' )' as TenKhachHang, MaDonHang from ViewDH  where [NgayKyDH] between '" + sDateFrom + "' and '" + sDateTo + "'";
		return cmDO.getTable( conn, ref tbDonHang, cmd );
	}
	
	public string getHangMucSanPham(	SqlConnection conn, 
										ref DataTable tbDonHang, 
										string sDonHangID )
	{
		string cmd = "Select * from HangMucSanPham  where DHID = '"+ sDonHangID +"'";
		return cmDO.getTable(conn, ref tbDonHang, cmd);
	}

	public string getVersion(SqlConnection conn,
										ref DataTable tbVersion,
										string sHMSPID )
	{
		string cmd = "Select distinct Version from DeXuatVatTu Where HMSPID = '" + sHMSPID + "'";
		return cmDO.getTable(conn, ref tbVersion, cmd);
	}
	

    public string getInfo(  SqlConnection conn,
                            string sDHID, string sHMSPID,
                        ref string sViTriLapDat, ref string sSoLuong )
    {
        DataTable temp = new DataTable();
        string cmdGetInfoSL = "";
        string sRet = "";
        if (sHMSPID != CommonHeader.BLANK)
        {
            cmdGetInfoSL = "select ViTri 'Info', SoLuong from HangMucSanPham "
                            + "Where DHID = N'" + sDHID + "' and HMSPID = N'" + sHMSPID + "'";
        }
        else
        {
            sViTriLapDat = "N/A";
            sSoLuong = "N/A";
            return CommonHeader.NO_ERROR;
        }


        sRet = cmDO.getTable(conn, ref temp, cmdGetInfoSL);

        if (sRet == CommonHeader.NO_ERROR)
        {
            if (temp.Rows.Count == 1)
            {
                sViTriLapDat = temp.Rows[0]["Info"].ToString();
                sSoLuong = temp.Rows[0]["SoLuong"].ToString();
                return CommonHeader.NO_ERROR;
            }
            else
            {
                sViTriLapDat = "N/A";
                sSoLuong = "N/A";
                return CommonHeader.NO_ERROR;
            }
        }
        return sRet;
    }

	public string getData(SqlConnection conn, ref DataTable tbDataGrid,                                    
								string sDHID, string sHMSPID, string sVersion ,
								int sortedCol, string sortDirect)
	{


		string cmd = "select * from ViewTongHopVatTu where ";
		string cmdCond  = "";
		if ( sDHID == CommonHeader.BLANK )
		{
			return "No Result";
		}		
		else
		{
			cmdCond = "DHID = N'" + sDHID + "'";
		}

		if ( sHMSPID != CommonHeader.BLANK )
		{
			cmdCond = "HMSPID = N'" + sHMSPID + "'";
		}
		
		cmd = string.Concat(cmd, cmdCond);
	

		if (sVersion != CommonHeader.BLANK)
		{
            cmd = "Select * from ViewDXVT where DHID = N'" + sDHID + "' "
					+ "AND HMSPID = N'" + sHMSPID + "'" 
					+ "AND Version = N'" + sVersion + "'"
					+ " ORDER BY " + colNames[ sortedCol ] + " " + sortDirect;
		}

		return cmDO.getTable( conn, ref tbDataGrid, cmd);
	}

	// Insert new record when list has not record to refer
	public String insertNewRecordGetID(SqlConnection conn,
										StructDeXuatVT stDXVT,
										ref String sNewID)
	{
		String cmd = "DeXuatVatTu_add ";

		cmd = String.Concat(cmd, "N'" + stDXVT.sHMSPID			+ "'");		
		cmd = String.Concat(cmd, ", N'" + stDXVT.sVTID			+ "'");
		cmd = String.Concat(cmd, ", N'" + stDXVT.dNgang.ToString()	+ "'");
		cmd = String.Concat(cmd, ", N'" + stDXVT.dCao.ToString()	+ "'");
		cmd = String.Concat(cmd, ", N'" + stDXVT.dSau.ToString()	+ "'");		
		cmd = String.Concat(cmd, ", N'" + stDXVT.dSL.ToString()		+ "'");		
		cmd = String.Concat(cmd, ", N'" + stDXVT.sBoPhanNhan		+ "'");
		cmd = String.Concat(cmd, ", N'" + stDXVT.sNgayDeXuat		+ "'");
		cmd = String.Concat(cmd, ", N'" + stDXVT.sGhiChu			+ "'");
		cmd = String.Concat(cmd, ", N'" + stDXVT.sVersion			+ "'");		
		cmd = String.Concat(cmd, ", N'" + stDXVT.sNVDeXuat			+ "'");
		
		return cmDO.InsertRecordGetID(conn, ref sNewID, cmd);
	}

	public string insertRecordGetID(SqlConnection conn,
										String sLastID,
										ref String sNewID)
	{
		String cmd = "FS_InsertDeXuatVatTuNew ";

		cmd = String.Concat(cmd, " '" + sLastID + "'");

		return cmDO.InsertRecordGetID(conn, ref sNewID, cmd);

	}

	public string deleteRecord(SqlConnection conn, string sID)
	{
		return cmDO.DelereRecord(conn,
									"DeXuatVatTu",
									"DXVTID",
									sID);
	}
	
	
	public string UpdateRecordByID(SqlConnection conn,
									string sColUpdate,
									string sUpdateValue,
									string sIdValue)
	{
		return cmDO.UpdateRecordByID(conn,
								SqlHeader.SP_UPDATE_TABLE_BY_ID,
								TB_DXVT,
								sColUpdate,
								sUpdateValue,
								DXVTID,
								sIdValue);
	}
	

// End class
}

// End namespace
}
